package com.phuc.game.tankshooter.sprite;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.constants.TimeConstants;
import org.anddev.andengine.util.modifier.IModifier;

import com.phuc.game.tankshooter.MainActivity;

public class SmokeSprite extends AnimatedSprite implements TimeConstants {

	Scene scene;
	boolean isRemoved;
	float alpha;
	float scale;
	
	float width;
	float height;

	public SmokeSprite(float pX, float pY, float width, float height,
			TiledTextureRegion pTiledTextureRegion, Scene scene) {
		super(pX - width / 2, pY - height / 2, width, height, pTiledTextureRegion);
		this.width = width;
		this.height = height;

		this.scene = scene;
		isRemoved = false;
		alpha = 1.0f;
		scale = 1.0f;
		
		AlphaModifier modifier = new AlphaModifier(2.5f, 1f, 0f, new IEntityModifier.IEntityModifierListener() {
			@Override
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) { }
			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
				MainActivity.getInstant().runFromUpdateThread(new Runnable() {
					@Override
					public void run() {
						SmokeSprite.this.detachSelf();
						SmokeSprite.this.setIgnoreUpdate(true);
						SmokeSprite.this.clearEntityModifiers();
						SmokeSprite.this.clearUpdateHandlers();
					}
				});
			} 
		});
		
		ScaleModifier scaleModifier = new ScaleModifier(2.5f, 1, 2);
		this.registerEntityModifier(new ParallelEntityModifier(modifier, scaleModifier));
	}

	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);

//		alpha -= 0.01f;
//		scale += 0.01;
//		this.setAlpha(alpha);
//		this.setScale(scale);
//		
//		if (alpha <= 0) {
//			MainActivity.getInstant().runFromUpdateThread(new Runnable() {
//				@Override
//				public void run() {
//					SmokeSprite.this.detachSelf();
//					SmokeSprite.this.setIgnoreUpdate(true);
//					SmokeSprite.this.clearEntityModifiers();
//					SmokeSprite.this.clearUpdateHandlers();
//				}
//			});
//		}
	}
}
